//
//  FindDetailViewController.m
//  KEREN
//
//  Created by MW on 2023/4/23.
//

#import "FindDetailViewController.h"
#import <WebKit/WebKit.h>

@interface FindDetailViewController ()<WKNavigationDelegate,WKUIDelegate>

@property (nonatomic, strong)WKWebView *webView;
@property (nonatomic, strong)WKWebViewConfiguration *webConfig;


@end

@implementation FindDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavViewWithRightTitle:@"" navColorIswhite:YES withTitle:_model.title];
    [self reloadDetaDetail];
    // Do any additional setup after loading the view.
}

- (void)reloadDetaDetail{
    WEAKSELF
    [[KRAlartManager shareInstance] showLodingAlertView:@"努力加载中..."];
    [NetworkingTool POST:@"/v1/app/vehicleSkill/detail" parameters:@{@"id":@(_model.Id)} isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        if([response[@"code"] intValue] == 0){
            if([response[@"data"] isKindOfClass:[NSDictionary class]]){
                NSDictionary *dict = response[@"data"];
                weakSelf.model = [KRFindListModel initWithDictionary:dict];
                [weakSelf.view addSubview:self.webView];
            }
        }else{
            
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        //[KRAlartManager shareInstance] showErrorAlert:<#(nonnull NSString *)#>
    }];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [[KRAlartManager shareInstance] hidenLodingImageView];
}

- (WKWebView *)webView{
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 56 + STATUSBAR_HEIGHT, DEVICE_WIDTH, DEVICE_HEIGHT - 56 - STATUSBAR_HEIGHT) configuration:self.webConfig];
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        if (@available(iOS 11.0, *)) {
           [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
        }
        [[KRAlartManager shareInstance] showLodingAlertView:@"努力加载中..."];
        self.content = _model.content;
        if(![_model.content containsString:@"img"]){
            NSString *urlStr = [NSString stringWithFormat:@"%@%@%@",self.content,@"<meta charset=\"UTF-8\">\n",@"<meta name=\"viewport\" content=\"width=\(device-width), initial-scale=1\"/>"];
            //
             [_webView loadHTMLString:urlStr baseURL:nil];
        }else{
            NSString *urlStr = [NSString stringWithFormat:@"<html> \n"
                                                   "<head> \n"
                                                   "<style type=\"text/css\"> \n"
                                                   "body {font-size:20px;}\n"
                                                   "</style> \n"
                                                   "</head> \n"
                                                   "<body>"
                                                   "<script type='text/javascript'>"
                                                   "window.onload = function(){\n"
                                                   "var $img = document.getElementsByTagName('img');\n"
                                                   "for(var p in  $img){\n"
                                                      " $img[p].style.width = '100%%';\n"
                                                       "$img[p].style.height ='auto'\n"
                                                   "}\n"
                                                   "}"
                                                   "</script>%@"
                                                   "</body>"
                                                   "</html>",self.content];
            [_webView loadHTMLString:urlStr baseURL:nil];

        }
    }
    return _webView;
}

- (WKWebViewConfiguration *)webConfig{
    if(!_webConfig){
        _webConfig = [[WKWebViewConfiguration alloc] init];
        WKPreferences *preference = [[WKPreferences alloc]init];
        preference.minimumFontSize = 0;
        preference.javaScriptEnabled = YES;
        preference.javaScriptCanOpenWindowsAutomatically = YES;
        _webConfig.preferences = preference;
        _webConfig.allowsInlineMediaPlayback = YES;
  
        _webConfig.processPool = [[WKProcessPool alloc]init];
    }
    return _webConfig;
}

@end
